package meconsea.life.admin.web;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import meconsea.life.admin.accessor.IndicateAccessor;
import meconsea.life.admin.entity.Indicate;
import meconsea.life.util.Constant;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;
import org.json.JSONObject;

import com.google.inject.Inject;
import com.opensymphony.xwork2.ActionSupport;

public class IndicateAct extends ActionSupport {
	private final static Logger log = Logger.getLogger(IndicateAct.class);
	private String parentCls;
	private String clsCode;
	private String clsName;
	private String childCls;
	private int clsLevel;
	private String call;
	public int getClsLevel() {
		return clsLevel;
	}
	public void setClsLevel(int clsLevel) {
		this.clsLevel = clsLevel;
	}
	public String getCall() {
		return call;
	}
	public void setCall(String call) {
		this.call = call;
	}

	@Inject
	private IndicateAccessor indicateAccessor;
	public String getParentCls() {
		return parentCls;
	}
	public void setParentCls(String parentCls) {
		this.parentCls = parentCls;
	}
	public String getClsCode() {
		return clsCode;
	}
	public void setClsCode(String clsCode) {
		this.clsCode = clsCode;
	}
	public String getClsName() {
		return clsName;
	}
	public void setClsName(String clsName) {
		this.clsName = clsName;
	}
	public String getChildCls() {
		return childCls;
	}
	public void setChildCls(String childCls) {
		this.childCls = childCls;
	}
	public IndicateAccessor getIndicateAccessor() {
		return indicateAccessor;
	}
	public void setIndicateAccessor(IndicateAccessor indicateAccessor) {
		this.indicateAccessor = indicateAccessor;
	}
	
	/**
	 * 
	 * @return
	 */
	public String execute(){
		//
		HttpServletResponse response = ServletActionContext.getResponse(); 
		HttpServletRequest request = ServletActionContext.getRequest();
		response.setContentType("text/xml");
		response.setHeader("Cache-Control", "no-cache");
		response.setCharacterEncoding("UTF-8");
		PrintWriter pw = null;
		try{
			pw = response.getWriter();
			if("getChildren".equals(getCall())){
				Object params[] = {"parentCls",getParentCls()};
				List<Indicate> children = getIndicateAccessor().getEntitysNamed(Indicate.FIND_IN_PARENT,-1,0, params);
				//List<Indicate> children = getIndicateAccessor().getIndicateByParentCls(getParentCls());
				JSONArray resJ = new JSONArray();
				for(Indicate indItem : children){
					JSONArray inIJ = new JSONArray();
					inIJ.put(indItem.getClsCode());
					inIJ.put(indItem.getClsName());
					inIJ.put(indItem.getClsLevel());
					inIJ.put(indItem.getClsOrder());
					inIJ.put(indItem.getParentCls());
					resJ.put(inIJ);
				}
				pw.write("{\"result\":\"1\",\"msg\":"+resJ.toString()+"}");
			}else if("add".equals(getCall())){
				Indicate newInd = new Indicate();
				newInd.setClsCode(getClsCode());
				newInd.setClsName(getClsName());
				newInd.setParentCls(getParentCls());
				getIndicateAccessor().addIndicate(newInd);
				return this.SUCCESS;
			}else if("update".equals(getCall())){
				Indicate upInd = getIndicateAccessor().getIndicateByClsCode(getClsCode());
				upInd.setClsName(getClsName());
				getIndicateAccessor().save(upInd);
				return this.SUCCESS;
			}else if("del".equals(getCall())){
				Indicate upInd = getIndicateAccessor().getIndicateByClsCode(getClsCode());
				getIndicateAccessor().removeIndicate(upInd);
				return this.SUCCESS;
			}else if("getInfo".equals(getCall())){
				Indicate ind = getIndicateAccessor().getIndicateByClsCode(getClsCode());
				if(ind == null){
					pw.write("{\"result\":\"0\",\"msg\":\"没找到相关的指标\"}");
				}else{
					JSONObject jInd = new JSONObject();
					jInd.put("clsCode", ind.getClsCode());
					jInd.put("clsName", ind.getClsName());
					jInd.put("clsLevel", ind.getClsLevel());
					jInd.put("clsOrder", ind.getClsOrder());
					jInd.put("parentCls", ind.getParentCls());
					pw.write("{\"result\":\"1\",\"msg\":"+jInd.toString()+"}");
				}
			}else if("ui".equals(getCall())){
				return this.SUCCESS;
			}else{
				log.info("return before");
				return Constant.ADMIN_LOGIN;
			}
		}catch(Exception e){
			log.error("指标操作出现异常.",e);
			pw.write("{\"result\":\"0\",\"msg\":"+e.getMessage()+"}");
		}
		return null;
	}
}
